package com.ruoyi.rsikManage.service.Impl;

import com.ruoyi.common.GenericMethodsService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.rsikManage.domain.AlarmEvent;
import com.ruoyi.rsikManage.domain.ReportApproval;
import com.ruoyi.rsikManage.mapper.ReportApprovalMapper;
import com.ruoyi.rsikManage.mapper.StatisticalAnalysisMapper;
import com.ruoyi.rsikManage.service.ReportApprovalService;
import org.apache.commons.io.output.BrokenWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ReportApprovalServiceImpl implements ReportApprovalService {
    @Autowired
    ReportApprovalMapper reportApprovalMapper;

    @Autowired
    StatisticalAnalysisMapper statisticalAnalysisMapper;

    @Autowired
    GenericMethodsService genericMethodsService;
    @Override
    public int addReportApproval(ReportApproval reportApproval) {
//        reportApproval.setApproverState("0");
        int i= 0 ;
        i= reportApprovalMapper.insertReportApproval(reportApproval);
        Long unitCode = reportApproval.getUnitCode();

            if(reportApproval.getApproverState().equals("1")){
                String apeId = reportApproval.getApeId();
                Date startTime = reportApproval.getStartTime();
                Date endTime = reportApproval.getEndTime();

                AlarmEvent alarmEvent = new AlarmEvent();
                alarmEvent.setUnitCode(unitCode);
                alarmEvent.setDeviceId(apeId);
                alarmEvent.setStartTime(startTime);
                alarmEvent.setEndTime(endTime);
                alarmEvent.setFilter("1");

                 i = statisticalAnalysisMapper.updateAlarmInfoByTime(alarmEvent);

                System.out.println("已报备影响行数："+i);


            }

        return i;
    }

    @Override
    public List<ReportApproval> selectReportApprovalList(ReportApproval reportApproval) {

        List<ReportApproval> reportApprovals = reportApprovalMapper.selectReportApprovalList(reportApproval);


        Map<Long, SysDept> deptMap = genericMethodsService.getDeptMap();
        // 遍历 reportApprovals 列表，根据 unitCode 匹配 deptName 并赋值给 unitName
        for (ReportApproval report : reportApprovals) {
            SysDept dept = deptMap.get(report.getUnitCode());
            if (dept != null) {
                report.setUnitName(dept.getDeptName());
            }
        }

        return reportApprovals;
    }



    @Override
    public ReportApproval selectReportApprovalById(Integer id) {
        ReportApproval reportApproval = reportApprovalMapper.selectReportApprovalById(id);
        Map<Long, SysDept> deptMap = genericMethodsService.getDeptMap();
        SysDept dept = deptMap.get(reportApproval.getUnitCode());
        if (dept != null) {
            reportApproval.setUnitName(dept.getDeptName());
        }
        return reportApproval;
    }

    @Override
    public int updateReportApproval(ReportApproval reportApproval) {
        //将原来的ReportApproval删除
        reportApprovalMapper.deleteReportApprovalById(reportApproval.getId());
        //获取原来的ReportApproval的创建时间
        Date createDate = reportApprovalMapper.getCreateDate(reportApproval.getId());
        reportApproval.setCreateDate(createDate);
        Long unitCode = reportApproval.getUnitCode();

        //新增ReportApproval
        int row = reportApprovalMapper.insertReportApproval(reportApproval);
        //审批通过 将报警表中符合条件的记录改为 已报备
        if(reportApproval.getApproverState().equals("1")){
            String apeId = reportApproval.getApeId();
            Date startTime = reportApproval.getStartTime();
            Date endTime = reportApproval.getEndTime();

            AlarmEvent alarmEvent = new AlarmEvent();
            alarmEvent.setUnitCode(unitCode);
            alarmEvent.setDeviceId(apeId);
            alarmEvent.setStartTime(startTime);
            alarmEvent.setEndTime(endTime);
            alarmEvent.setFilter("1");

            int i = statisticalAnalysisMapper.updateAlarmInfoByTime(alarmEvent);

            System.out.println("已报备影响行数："+i);


        }
        return row;

    }

    @Override
    public int deleteReportApproval(ReportApproval reportApproval) {

        //将原来的ReportApproval删除
        int row = reportApprovalMapper.deleteReportApprovalById(reportApproval.getId());
        //该报备删除 批量将报警表中符合条件的记录改为 未报备
        if (row>0){
            String apeId = reportApproval.getApeId();
            Date startTime = reportApproval.getStartTime();
            Date endTime = reportApproval.getEndTime();
            Long unitCode = reportApproval.getUnitCode();

            AlarmEvent alarmEvent = new AlarmEvent();
            alarmEvent.setUnitCode(unitCode);
            alarmEvent.setDeviceId(apeId);
            alarmEvent.setStartTime(startTime);
            alarmEvent.setEndTime(endTime);
            alarmEvent.setFilter("0");

            int i = statisticalAnalysisMapper.updateAlarmInfoByTime(alarmEvent);

            System.out.println("影响行数："+i);
        }



        return row;
    }

    @Override
    public int deleteReportApprovalById(Integer id) {
        //设置删除时间、删除人，同时将删除标志置为 1
        return reportApprovalMapper.deleteReportApprovalById(id);
    }
    @Override
    public int deleteReportApprovalByIds(Integer[] ids) {
        //设置删除时间、删除人，同时将删除标志置为 1
        return reportApprovalMapper.deleteReportApprovalByIds(ids);
    }

}
